package cn.net.isir.controller.application;

import cn.net.isir.common.ResultObject;
import cn.net.isir.core.IgnoredTicket;
import cn.net.isir.entity.PrintScope;
import cn.net.isir.entity.Printer;
import cn.net.isir.service2.PrinterService;
import cn.net.isir.utils.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author liuxubo
 * @filename PrinterManageController.java
 * @package cn.net.isir.controller.application.printer
 * @email lxb@isir.net.cn
 * @date 2018/12/26
 * @comment 打印机管理
 * @since 1.8
 */
@RestController
@RequestMapping("/app/printer")
public class PrinterManageController {
    @Resource
    private PrinterService printerService;

    /**
     * 获取打印列表
     *
     * @param shopId 店铺id
     * @return 列表
     */
    @IgnoredTicket
    @RequestMapping("/list/{shopId}")
    public ResultObject getPrintScopeList(@PathVariable String shopId) {
        if (StringUtils.isBlank(shopId)) {
            return new ResultObject(500, "获取店铺信息失败", null);
        }
        PrintScope printScope = new PrintScope();
        printScope.setShopId(shopId);
        Printer printer = new Printer();
        printer.setShopId(shopId);
        List<Printer> printScopeList = printerService.selectList(printer);
        return new ResultObject(200, "success", printScopeList);
    }

    /**
     * 添加打印区域
     *
     * @param printer 打印实体
     * @param request 请求
     * @return 结果
     */
    @IgnoredTicket
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultObject addPrintScope(@RequestBody Printer printer, HttpServletRequest request) {
        if (StringUtils.isBlank(printer.getShopId())) {
            return new ResultObject(500, "店铺信息不存在", null);
        }
        printerService.insert(printer);
        if (StringUtils.isBlank(printer.getId())) {
            return new ResultObject(500, "网络错误,请稍后再试", null);
        }
        return new ResultObject(200, "success", printer.getId());
    }

    /**
     * 删除打印区域
     *
     * @param printerId 打印实体id
     * @param request   请求
     * @return 结果
     */
    @IgnoredTicket
    @RequestMapping(value = "/delete/{printerId}", method = RequestMethod.DELETE)
    public ResultObject deletePrintScope(@PathVariable String printerId, HttpServletRequest request) {
        return new ResultObject(200, "success", printerService.delete(printerId));
    }
}
